package com.budwk.app.miniapp.common;

import org.nutz.ioc.loader.annotation.IocBean;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@IocBean
public class ExecutorConfig {

    /**
     * 该线程池只做查询后使用
     * @return
     */
    @IocBean(name = "taskQueryExecutor")
    public ThreadPoolExecutor taskExecutor() {
        ThreadPoolExecutor executorService = new ThreadPoolExecutor(20, 30, 1000 * 60, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(2000), r -> {
            Thread thread = new Thread(r);
            thread.setName("query-taskQueryExecutor-thread");
            return thread;
        }, new ThreadPoolExecutor.CallerRunsPolicy());
        return executorService;
    }

    /**
     * 该线程池只做更新(更新，删除等均可)使用
     * @return
     */
    @IocBean(name = "taskUpdateExecutor")
    public ThreadPoolExecutor taskUpdateExecutor() {
        ThreadPoolExecutor executorService = new ThreadPoolExecutor(10, 20, 1000 * 60, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(2000), r -> {
            Thread thread = new Thread(r);
            thread.setName("query-taskUpdateExecutor-thread");
            return thread;
        }, new ThreadPoolExecutor.CallerRunsPolicy()); // 修改拒绝策略为使用当前线程执行
        return executorService;
    }
}
